/************************************************
 * Copyright (c) 2007 ~ 2009, 
 * All rights reserved.
 * Version   : 1.0
 * Author    : lengsh@gmail.com 
 * Date      : 2009.2.1
 ***********************************************/

#ifndef   __NETPROXY_CONNMANGER_H___
#define   __NETPROXY_CONNMANGER_H___


#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <syslog.h>
#include <signal.h>
#include <unistd.h>
#include <map>
#include <pthread.h>
#include "IConnection.h"
using namespace std;
using namespace netproxy;

typedef struct ConnStruct{
	IConnection *pCon;
#ifdef _THREAD_SAFE
	pthread_mutex_t lock;
#endif
} ConnStruct;

class ConnManager{
	protected:
        int m_iPartition;
		map < int, ConnStruct * > m_Conn_Map;
        int m_iType;
        int m_iRestart;
//        int m_iReqCount;
//        bool m_bRestart; 
    protected:
		void _Lock(int idx);
		void _UnLock(int idx);
		void trim(string& str);

	public:
	ConnManager(int type):m_iPartition(0),m_iType(type) {  m_iRestart = 200; }
	virtual ~ConnManager();

        int SetMaxRequest(int max){ max > 0 ?  m_iRestart = max : m_iRestart = 1000 ; return m_iRestart;  }
//        void SetAutoRestart( bool bs){ m_bRestart = bs;  }
	virtual bool Initialize(const char *conf) ;
        virtual int ResetStatus(const char *ip, int port);
	virtual int Proxy(int key, const char *data, unsigned dlen, char **wbuf,unsigned &wsize, unsigned &vlen, bool bReply) ;

};


#endif  // __PANDA_MEMCACHE_H___

